﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;

namespace WechatUtility
{
    public class WebConfigHelper
    {
        private static string configPath = Utilities.GetRootPath() + "\\web.config";

        /// <summary>
        /// 返回WebConfig中appSettings设置
        /// <param name="key">属性名</param>
        /// </summary>
        /// <returns></returns>        
        public static string GetAppSet(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 添加WebConfig中appSettings属性
        /// <param name="key">属性名</param>
        /// <param name="value">值</param>
        /// </summary>
        /// <returns></returns>        
        public static void AddAppSet(string key, string value)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
            AppSettingsSection app = config.AppSettings;
            app.Settings.Add(key, value);
            config.Save(ConfigurationSaveMode.Minimal);
        }

        /// <summary>
        /// 修改WebConfig中appSettings属性
        /// <param name="key">属性名</param>
        /// <param name="value">值</param>
        /// </summary>
        /// <returns></returns>        
        public static void ModifyAppSet(string key, string value)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
            AppSettingsSection app = config.AppSettings;
            app.Settings[key].Value = value;
            config.Save(ConfigurationSaveMode.Modified);
        }

        /// <summary>
        /// 删除WebConfig中appSettings属性
        /// <param name="key">属性名</param>
        /// </summary>
        /// <returns></returns>        
        public static void DelAppSet(string key)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
            AppSettingsSection app = config.AppSettings;
            app.Settings.Remove("p");
            config.Save(ConfigurationSaveMode.Modified);
        }
    }
}
